<template>
	<view class="page">
		<template v-if="userStore.userInfo.agentLevel !== 2">
			<view class="menu-list flex item-center justify-center">
				<template v-for="item in menu" :key="item.id">
					<view class="menu-item" v-if="item.show!=false" :class="{'active':menuId == item.id}"
						@click="selMenu(item)">
						{{item.name}}
						<view class="sel-line" v-if="menuId == item.id"></view>
					</view>
				</template>
			</view>
			<Agent v-if="menuId == 1" :agentCount="agentCount" :waitAuditAgentCount="waitAuditAgentCount" @updateNum="getNum" @emitImg='emitImg'
				 @getGroup="getGroup" @updateList="getNum"></Agent>
			<Business :businessCount="businessCount" v-if="menuId == 2"></Business>
		</template>
		<Agent1 v-else />
	</view>
</template>

<script setup>
	import {
    	onShow,
		onShareAppMessage
	} from '@dcloudio/uni-app';
	import {
		ref,
		onMounted
	} from 'vue'
	import Agent from '@/pagesH/components/agent.vue'
	import Agent1 from './components/agent1.vue'
	import Business from './components/business.vue';
	import {
		useUserStore
	} from '@/stores/modules/user.js'
	import {
		businessAgentIndex
	} from '@/api/business.js'
	const userStore = useUserStore()
	const menu = ref([{
		name: '我的经纪人',
		id: 1,
		num:0,
		show:false
	}, {
		name: '我的策划人',
		id: 2,
		num:0,
		show:false
	}])

	const menuId = ref()
	const shareImg = ref('')
	const inviteData = ref({})
	const agentCount = ref(0)
	const waitAuditAgentCount = ref(0)
	const businessCount = ref(0)
	let groupId = ''
	let name = ''
	let inviteCode = ''
	const getGroup = (id, name, code) => {
		groupId = id || ''
		name = name || ''
		inviteCode = code || ''
	}
	const selMenu = (item) => {
		menuId.value = item.id
	}
	const emitImg = (data) => {
		shareImg.value = data.img
		inviteData.value = data.inviteData
	}
	const getNum = async () => {
		await userStore.getUserInfo();		
		let result = await businessAgentIndex()
		if(result){
			agentCount.value = result.agentCount
			waitAuditAgentCount.value = result.waitAuditAgentCount
			businessCount.value = result.businessCount
			menu.value = [{
				name: '我的经纪人',
				id: 1,
				num:result.agentCount,
				show:result.agentShowState
			}, {
				name: '我的策划人',
				id: 2,
				num: result.businessCount,
				show: result.businessShowState
			}]
			if(result.agentShowState){
				menuId.value = 1
			}else if(result.businessShowState){
				menuId.value = 2
			}
		}
	}
	// onMounted(() => {
	// 	getNum()
	// 	uni.hideShareMenu()
	// })
  onShow(() => {
    getNum()
  })
	onShareAppMessage((res) => {
		return {
			title: `${name}邀请您成为TA的经纪人`,
			path: `/applyPages/bindBusiness/bindBusiness?code=${inviteCode}&groupId=${groupId}`,
			imageUrl: res.target.dataset.filepath
		}
	})
</script>

<style lang="scss" scoped>
	.menu-list {
		background-color: #ffffff;
		padding: 10px 0 0 0;
		position: relative;
		z-index: 4;
		.menu-item {
			width: 35%;
			text-align: center;
			position: relative;
			font-size: 30rpx;
			font-weight: 400;
			line-height: 22px;
			height: 30px;
			color: rgba(51, 51, 51, 0.6);

			&.active {
				color: rgba(51, 51, 51, 1);
			}

			.sel-line {
				position: absolute;
				width: 19px;
				height: 4px;
				background: rgba(250, 69, 67, 1);
				border-radius: 4px;
				left: 0;
				right: 0;
				bottom: 0;
				margin: auto;
			}
		}
	}
</style>